/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import bdclass.Persona;
import bdclass.Sucursal;
import bdclass.tipoP_persona;
import bdclass.usuario;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author DEMRR
 */
@WebServlet(name = "validarUs", urlPatterns = {"/validarUs"})
public class validarUs extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
            
            try
{
	Class.forName("org.postgresql.Driver");
	String usuario=String.valueOf(request.getParameter("usuario"));
        String pass=String.valueOf(request.getParameter("pass"));
        usuario us=new usuario();
        ResultSet rs=us.SelectUs();
                
        Persona per=new Persona();
        ResultSet rs2=per.SelectPersona();
                
        tipoP_persona tp=new tipoP_persona();
        ResultSet rs3=tp.SelectPERSONA_TIPOP();
        
        Sucursal suc=new Sucursal();
        ResultSet rs4=suc.SelectSucursal();
        int flag=0;
        int nivel;
        String persona;
        int tipop=0;


	while(rs.next())
            {
                    if(usuario.equals(rs.getString("nombre_usuario"))&&pass.equals(rs.getString("contrasena")))
                            {

                                    HttpSession session = request.getSession(true);
                                    session.setAttribute("usuario",rs.getString("nombre_usuario"));
                                    session.setAttribute("id_usuario",rs.getInt("id_usuario"));

                                            while (rs2.next())
                                            {
                                            persona=rs2.getString("nombre");

                                            if(rs2.getInt("id_persona")==(rs.getInt("PERSONA_id_persona")))
                                            {
                                                session.setAttribute("persona",persona);
                                            }
                                            }

                                            while (rs3.next())
                                            {

                                            if(rs.getInt("PERSONA_id_persona")==rs3.getInt("PERSONA_id_persona"))
                                            {
                                                tipop=rs3.getInt("TIPO_PERSONA_id_tipo");
                                            }
                                            }

                                    nivel=rs.getInt("PERMISO_USUARIO_id_permiso");
                                    flag=1;
                                    
                                    out.println(" <HEAD>");
                                    out.println(" <META http-equiv=Content-Type'");
                                    out.println(" content='text/html; charset=UTF-8'>");
                                    out.println(" <TITLE>San Martin Bakery</TITLE>");
                                    out.println(" </HEAD>");                           
                                    out.println(" <BODY style=\'background: #CAA765 url(images/wrapper-bg.jpg)  \'>");
                                    out.println("<div style=\'float:middle bgcolor=#000000;margin-left:550px; margin-right:500px;margin-top:150px; height=auto;\'>");
                                    out.println("<H1>bienvenido</H1>");

                                    if(tipop==1)
                                    {
                                        out.println(" <H2>Cliente Especial</H2>");
                                        out.println("<a href=pedido.jsp  rel=register>Home</a>");
                                        
                                        out.println("&nbsp;&nbsp;&nbsp;&nbsp;");                                      
                                        out.println("<a href=login.jsp  rel=register>Salir</a>");                                                                                



                                        out.println(" <p></p>");



                                        out.println("</div>"); 
                                        out.println("<div style=\'float:middle bgcolor=#000000;margin-left:550px; margin-right:500px;margin-top:25px;\'>");
                                        out.println(" <img src=\'images/admin.jpg\'>");
                                        out.println(" <p></p>");
                                        out.println("<a href=pedido.jsp  rel=register>Ir</a>");
                                        out.println("</div>'"); 

                                        out.println(" </BODY>");
                                        out.println("</HTML>");
                                        out.close();
                                        
                                    }
                                    
                                    else{
                                        switch(nivel)
                                            {
                                                case 1: out.println(" <H2>Administrador</H2>");                                        
                                                        out.println("<a href=admin.jsp  rel=register>Admin</a>");
                                                        break;
                                                case 2: out.println(" <H2>Administrador 2</H2>");
                                                        out.println("<a href=admin1.jsp  rel=register>Admin</a>");
                                                        break;
                                                case 3: out.println(" <H2>Administrador 3</H2>");
                                                        out.println("<a href=admin2.jsp  rel=register>Admin</a>");
                                                        break;

                                            }
                                        
                                        out.println("&nbsp;&nbsp;&nbsp;&nbsp;");                                      
                                        out.println("<a href=login.jsp  rel=register>Salir</a>");                                                                                



                                        out.println(" <p></p>");

                                        out.println("<select size=1 name=select1 onChange=confirmar\'()\'>");
                                        
                                        while(rs4.next())
                                        {
                                            out.println("<option value='"+rs4.getInt("id_sucursal")+"'>"+rs4.getString("nombre_sucursal")+"</option>");
                                        }
                                        
                                        
                                        out.println("</select> ");

                                        out.println("</div>"); 
                                        out.println("<div style=\'float:middle bgcolor=#000000;margin-left:550px; margin-right:500px;margin-top:25px;\'>");
                                        out.println(" <img src=\'images/admin.jpg\'>");
                                        out.println(" <p></p>");
                                        out.println("<a href=pedido.jsp  rel=register>Ir</a>");
                                        out.println("</div>'"); 

                                        out.println(" </BODY>");
                                        out.println("</HTML>");
                                        out.close();

                                    }
                                    
                            }
                    
                    
                    }

                    

                    

                    if(flag==0)
                    {	
                            out.println(" <HEAD>");
                            out.println(" <META http-equiv=Content-Type'");
                            out.println(" content='text/html; charset=UTF-8'>");
                            out.println(" <TITLE>San Martin Bakery</TITLE>");
                            out.println(" </HEAD>");                           
                            out.println(" <BODY style=\'background: #CAA765 url(images/wrapper-bg.jpg)  \'>");
                            out.println("<div style=\'margin-left:550px; margin-right:500px;margin-top:150px;\'>");
                            out.println("<H2>USUARIO O CONTRASEÑA INCORRECTOS</H2>");
                            out.println("<input type=submit name=boton id=boton value='Intentar Otra Vez'  onClick=location.href='http://localhost:8080/appSanMartin/login.jsp'; />");
                            out.println("</div>");
                            out.println(" </BODY>");
                            out.println("</HTML>");
                    }



            }
            catch (Exception ex) {
                System.err.println(ex.toString());
            }
            
            
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
